package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    /**
     * 分页查询spu
     * @param page      页码
     * @param rows      每页数量
     * @param saleable      是否上架
     * @param categoryId     商品分类id
     * @param brandId         品牌id
     * @param id             spu的id
     * @return   返回查询spu的分页数据
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id) {
        //查询
        PageDTO<SpuDTO> pageDTO = spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id);
        //返回
        return ResponseEntity.ok(pageDTO);
    }

    /**
     * 新增商品
     * @param spuDTO        录入的商品对象
     * @return      wu
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        //添加
        spuService.saveGoods(spuDTO);
        //返回
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 更新商品
     * @param spuDTO    修改后的商品对象
     * @return          无
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        //更新
        spuService.updateGoods(spuDTO);
        //返回
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改商品上下架
     * @param id            商品id
     * @param saleable      上下架信息
     * @return          无
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam(value = "id",required = false)Long id,
            @RequestParam(value = "saleable",required = false)Boolean saleable) {
        //修改
        spuService.updateSaleable(id, saleable);
        //返回
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询商品
     * @param id    商品id
     * @return      spu商品对象
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id) {
        //查询
        Spu spu = spuService.getById(id);
        //转换返回
        return ResponseEntity.ok(new SpuDTO(spu));
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id       spu商品id
     * @return          包含sku、spuDetail等的SpuDto对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {
        //查询
        SpuDTO spuDTO = spuService.queryGoodsById(id);
        //返回
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据id查询商品详情
     * @param id   商品id
     * @return       商品详情
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        //查询
        SpuDetail spuDetail = detailService.getById(id);
        //转换,返回
        return ResponseEntity.ok(new SpuDetailDTO(spuDetail));
    }

    /**
     * 根据spuID查询sku的集合
     * @param id    商品id
     * @return      sku对象集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkusBySpuId(@RequestParam("id") Long id) {
        //查询
        List<Sku> skuList = skuService.query().eq("spu_id", id).list();
        //转换，返回
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuList));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param spuId     商品id
     * @param searching     是否参与搜索
     * @return          规格参数的值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValuesBySpuId(
            @RequestParam("id")Long spuId,
            @RequestParam(value = "searching",required = false)Boolean searching) {
        //查询
        List<SpecParamDTO> list = spuService.querySpecValuesBySpuId(spuId, searching);
        //返回
        return ResponseEntity.ok(list);
    }

    /**
     * 根据id批量查询sku
     * @param ids    sku的id集合
     * @return        sku信息集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkusByIds(@RequestParam("ids")List<Long> ids) {
        //查询
        List<Sku> skuList = skuService.listByIds(ids);
        //转换，返回
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuList));
    }

    /**
     * 批量减库存
     * @param cartMap    要扣减库存的skuId及数量集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }
}